var member_id = gc_id_1 = store_id = likekey = goods_id = '';
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	member_id = plus.storage.getItem('id');
	store_id = plus.storage.getItem('store_id');
	member_name = plus.storage.getItem('name');
	$('#search').keydown(function(e){
		if(e.which == 13) {
			likekey = $('#search').val();
			if(!likekey){toast('无法进行空搜索');return;}
			goods_inf(likekey)
		}
//		console.log(222);
	})
	$('.search_goods').on('click',function(e){
		likekey = $('#search').val();
		if(!likekey){toast('无法进行空搜索');return;}
		goods_inf(likekey)
	})
	
},false)

function goods_inf(likekey){
	plus.nativeUI.showWaiting('搜索中...');
	$.ajax({ 
		type:"post",
		url:apiRoot + "?act=goods&op=goods_search",
		data:{
			store_id:store_id,
			likekey:likekey
		}, 
		dataType:'json',
		success:function(data){
			plus.nativeUI.closeWaiting();
			console.log(JSON.stringify(data));
			if(data.code == 200){
				if(data.datas.length > 0){
					var html = '';
					$.each(data.datas, function(k,v) {
						html += '<ul class="mui-table-view goods_detail" data-goods_id="'+ v.goods_id +'" data-store_id="'+ v.store_id +'" data-gc_id_1="'+ v.gc_id_1 +'">'+
									'<li class="mui-table-view-cell frien_img">'+
										'<div class="home_top">'+
											'<img src="'+ pics + v.store_id + '/' + v.goods_image +'"/>'+
										'</div>'+
										'<div class="frien_bq">'+																				
											'<div class="mui-ellipsis-2">'+ v.goods_name +'</div>'+															
											'<b class="red">￥25.00<span class="mui-h6 mui-pull-right">销量：'+ v.goods_salenum +'</span></b>	'+												
										'</div>'+
									'</li>'+
								'</ul>';
					});
					$('.Goods').html(html);
				}else{
					toast('没有相关搜索结果');
				}
			} 
			$('.goods_detail').unbind('tap'); 
			$('.goods_detail').on('tap',function(){
				var _this = $(this);
				goods_id = _this.attr('data-goods_id');
				store_id = _this.attr('data-store_id');
				gc_id_1 = _this.attr('data-gc_id_1');
				console.log(gc_id_1);
				if(gc_id_1 == '1059'){
//					console.log(1);
					goods_details(goods_id,store_id,'Travel_details.html');
				}else if(gc_id_1 == '1060'){
//					console.log(2);
					goods_details(goods_id,store_id,'Driving_details.html');
				}else if(gc_id_1 == '1062'){
//					console.log(3);
					goods_details(goods_id,store_id,'Flea_details.html');
				}else{
					goods_details(goods_id,store_id,'goods_details.html');
				}
				
			})

		},
		error:function(e){
			plus.nativeUI.closeWaiting();
			error();
		}
	});
}
function goods_details(goods_id,store_id,spage){
	var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
	var webviewContent= plus.webview.create(spage,spage,{},{goodsid:goods_id,store_id:store_id});//后台创建webview并打开show.html
	webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
        nwaiting.close(); //新webview的载入完毕后关闭等待框
        webviewContent.show("slide-in-right",300); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
    }, false);
}
